package com.example.controller;

import com.example.entity.Forum;
import com.example.entity.User;
import com.example.service.ForumService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

// 声明该类为Spring MVC的控制器，处理与论坛相关的请求
@Controller
// 定义该控制器处理的请求路径前缀为 /forum
@RequestMapping("/forum")
public class ForumController {
    // 自动注入ForumService实例，用于处理论坛业务逻辑
    @Autowired
    private ForumService forumService;
    @RequestMapping("/add")
    public String addForum(@Param("content") String content, HttpServletRequest request, Model model) {
        // 获取当前请求的会话
        HttpSession session = request.getSession();
        // 从会话中获取当前登录的用户信息
        User user = (User) session.getAttribute("login_user");
        // 调用ForumService的addForum方法添加新的论坛帖子
        int check = forumService.addForum(user.getUser_id(), content);
        List<Forum> forumList = forumService.listForum(user.getUser_id());
        model.addAttribute("forumList", forumList);
        System.out.println("add的list展示" + forumList);
        return "/Forum/forum";
    }


    @RequestMapping("/list")
    public String listForum(Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("login_user");
        // 调用ForumService的listForum方法获取指定用户的论坛帖子列表
        List<Forum> forumList = forumService.listForum(user.getUser_id());
        model.addAttribute("forumList", forumList);
        System.out.println("forumList" + forumList);
        return "/Forum/forum";
    }


    @RequestMapping("/listById")
    public String listById(Model model, HttpServletRequest request, @Param("user_id") int user_id) {
        HttpSession session = request.getSession();
        // 调用ForumService的listForumById方法获取指定用户的论坛帖子列表
        List<Forum> forumByIdList = forumService.listForumById(user_id);
        model.addAttribute("forumByIdList", forumByIdList);
        System.out.println("forumByIdList" + forumByIdList);
        return "/Forum/ownForum";
    }


    @RequestMapping("/delete")
    public String delete(Model model, HttpServletRequest request, @Param("share_id") int share_id, HttpSession session) {
        // 从会话中获取当前登录的用户信息
        User user = (User) session.getAttribute("login_user");
        // 调用ForumService的deleteForum方法删除指定的论坛帖子
        int check = forumService.deleteForum(share_id, user.getUser_id());
        // 调用ForumService的listForum方法获取指定用户的论坛帖子列表
        List<Forum> forumList = forumService.listForum(user.getUser_id());
        model.addAttribute("forumList", forumList);
        return "/Forum/ownForum";
    }
}